-- property_assign
-- Created by chengb Oct/30/2015
-- 通过获得道具领取任务

-- 内部函数声明
local whenItemBonus;

-- 关注获得物品的事件
EventMgr.register("dungeon_tasks/property_assign", event.BONUS_ITEM, function(args)
    whenItemBonus(args);
end);

-- 获得物品的事件回调
whenItemBonus = function(args)
    local classId = args.classId;
    local amount  = args.amount;

    if amount <= 0 then
        return;
    end

    local taskList = DungeonTaskM.getTasksByAssignRule("property_assign");
    for _, config in pairs(taskList) do
        repeat
            local id = config["id"];
            local assignArg = config["assign_arg"];
            if classId ~= assignArg["property_id"] then
                -- 不是需求的物品
                break;
            end

            -- 抽取目标
            local targetId = assignArg["target_id"];
            local extra = {};
            if type(targetId) == 'number' then
                extra["target_id"] = targetId;
            else
                local formulaName = targetId;
                extra["target_id"] = FormulaM.invoke(formulaName, ME.user);
            end

            -- 领取任务
            DungeonTaskM.assignTask(id, extra, amount);

        until true
    end
end

return {
};